Completed
Push — master ( 45d7a1...2503bc )
by Sander
11s
created

angular.controller(ꞌSettingsCtrlꞌ)   B

Complexity

Conditions 6
Paths 25

Size

Total Lines 274

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
c 2
b 1
f 0
nc 25
nop 12
dl 0
loc 274
rs 8.1463

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
27
	/**
28
	 * @ngdoc function
29
	 * @name passmanApp.controller:SettingsCtrl
30
	 * @description
31
	 * # SettingsCtrl
32
	 * Controller of the passmanApp
33
	 */
34
	angular.module('passmanApp')
35
		.controller('SettingsCtrl', ['$scope', '$rootScope', 'SettingsService', 'VaultService', 'CredentialService', '$location', '$routeParams', '$http', 'EncryptService', 'NotificationService', '$sce', '$translate',
36
			function ($scope, $rootScope, SettingsService, VaultService, CredentialService, $location, $routeParams, $http, EncryptService, NotificationService, $sce, $translate) {
37
				$scope.vault_settings = {};
38
				$scope.new_vault_name = '';
39
				$scope.active_vault = VaultService.getActiveVault();
40
				if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
41
					if (!$scope.active_vault) {
42
						$location.path('/');
43
						return;
44
					}
45
				} else {
46
					if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
47
						var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
48
						_vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
49
						VaultService.setActiveVault(_vault);
50
						$scope.active_vault = _vault;
51
					}
52
				}
53
54
				VaultService.getVault($scope.active_vault).then(function (vault) {
55
					vault.vaultKey = VaultService.getActiveVault().vaultKey;
56
					delete vault.credentials;
57
					VaultService.setActiveVault(vault);
58
					$scope.vault_settings = vault.vault_settings;
59
					if (!$scope.vault_settings.hasOwnProperty('pwSettings')) {
60
						$scope.vault_settings.pwSettings = {
61
							'length': 12,
62
							'useUppercase': true,
63
							'useLowercase': true,
64
							'useDigits': true,
65
							'useSpecialChars': true,
66
							'minimumDigitCount': 3,
67
							'avoidAmbiguousCharacters': false,
68
							'requireEveryCharType': true,
69
							'generateOnCreate': true
70
						};
71
					}
72
				});
73
74
75
				var btn_txt = $translate.instant('bookmarklet.text');
76
				var http = location.protocol, slashes = http.concat("//"), host = slashes.concat(window.location.hostname), complete = host + location.pathname;
77
				$scope.bookmarklet = $sce.trustAsHtml("<a class=\"button\" href=\"javascript:(function(){var a=window,b=document,c=encodeURIComponent,e=c(document.title),d=a.open('" + complete + "bookmarklet?url='+c(b.location)+'&title='+e,'bkmk_popup','left='+((a.screenX||a.screenLeft)+10)+',top='+((a.screenY||a.screenTop)+10)+',height=750px,width=475px,resizable=0,alwaysRaised=1');a.setTimeout(function(){d.focus()},300);})();\">" + btn_txt + "</a>");
78
79
80
				$scope.saveVaultSettings = function () {
81
					var _vault = $scope.active_vault;
82
					_vault.name = $scope.new_vault_name;
83
					_vault.vault_settings = angular.copy($scope.vault_settings);
84
					VaultService.updateVault(_vault).then(function () {
85
						//VaultService.setActiveVault(_vault);
86
						$scope.active_vault.name = angular.copy(_vault.name);
87
						NotificationService.showNotification($translate.instant('settings.saved'), 5000);
88
					});
89
				};
90
91
92
				$scope.tabs = [
93
					{
94
						title: $translate.instant('settings.general'),
95
						url: 'views/partials/forms/settings/general_settings.html'
96
					},
97
					{
98
						title: $translate.instant('settings.audit'),
99
						url: 'views/partials/forms/settings/tool.html'
100
101
					},
102
					{
103
						title: $translate.instant('settings.password'),
104
						url: 'views/partials/forms/settings/password_settings.html'
105
106
					},
107
					{
108
						title: $translate.instant('settings.import'),
109
						url: 'views/partials/forms/settings/import.html'
110
111
					},
112
					{
113
						title: $translate.instant('settings.export'),
114
						url: 'views/partials/forms/settings/export.html'
115
116
					},
117
					{
118
						title: $translate.instant('settings.sharing'),
119
						url: 'views/partials/forms/settings/sharing.html'
120
					}
121
				];
122
123
				$scope.currentTab = $scope.tabs[0];
124
125
				$scope.onClickTab = function (tab) {
126
					$scope.currentTab = tab;
127
				};
128
129
				$scope.isActiveTab = function (tab) {
130
					return tab.url === $scope.currentTab.url;
131
				};
132
133
				var getPassmanVersion = function () {
134
					var url = OC.generateUrl('apps/passman/api/internal/version');
135
					$http.get(url).then(function (result) {
136
						$scope.passman_version = result.data.version;
137
					});
138
				};
139
				getPassmanVersion();
140
141
				$scope.$watch(function () {
142
					return VaultService.getActiveVault();
143
				}, function (vault) {
144
					if (vault) {
145
						$scope.active_vault = vault;
146
					}
147
				});
148
149
				$rootScope.$on('logout', function () {
150
					$scope.selectedVault = false;
151
				});
152
153
				var getCurrentVaultCredentials = function (callback) {
154
					VaultService.getVault($scope.active_vault).then(callback);
155
				};
156
157
				$scope.startScan = function (minStrength) {
158
					getCurrentVaultCredentials(function (vault) {
159
						var results = [];
160
						for (var i = 0; i < vault.credentials.length; i++) {
161
							var c = angular.copy(vault.credentials[i]);
162
							if (c.password && c.hidden === 0) {
163
								try {
164
									c = CredentialService.decryptCredential(c);
165
									if (c.password) {
166
										var zxcvbn_result = zxcvbn(c.password);
167
										if (zxcvbn_result.score <= minStrength) {
168
											results.push({
169
												credential_id: c.credential_id,
170
												label: c.label,
171
												password: c.password,
172
												password_zxcvbn_result: zxcvbn_result
173
											});
174
										}
175
									}
176
								} catch (e) {
177
									console.warn(e);
178
								}
179
180
							}
181
							//@todo loop custom fields (if any and check secret fields
182
						}
183
						$scope.scan_result = results;
184
					});
185
				};
186
187
188
				$scope.cur_state = {};
189
190
191
				$scope.$on("$locationChangeStart", function (event) {
192
					if ($scope.change_pw) {
193
						if ($scope.change_pw.total > 0 && $scope.change_pw.done < $scope.change_pw.total) {
194
							if (!confirm($translate.instant('changepw.navigate.away.warning'))) {
195
								event.preventDefault();
196
							}
197
						}
198
					}
199
				});
200
201
202
				$scope.changeVaultPassword = function (oldVaultPass, newVaultPass, newVaultPass2) {
203
					$scope.error = '';
204
					if (oldVaultPass !== VaultService.getActiveVault().vaultKey) {
205
						$scope.error = $translate.instant('incorrect.password');
206
						return;
207
					}
208
					if (newVaultPass !== newVaultPass2) {
209
						$scope.error = $translate.instant('password.no.match');
210
						return;
211
					}
212
					SettingsService.setSetting('defaultVault', null);
213
					SettingsService.setSetting('defaultVaultPass', null);
214
					VaultService.getVault($scope.active_vault).then(function (vault) {
215
						jQuery('input').attr('disabled', true);
216
						jQuery('button').attr('disabled', true);
217
						var _selected_credentials = angular.copy(vault.credentials);
218
						$scope.change_pw = {
219
							percent: 0,
220
							done: 0,
221
							total: _selected_credentials.length
222
						};
223
						var changeCredential = function (index, oldVaultPass, newVaultPass) {
224
							var usedKey = oldVaultPass;
225
226
							if (_selected_credentials[index].hasOwnProperty('shared_key')) {
227
								if (_selected_credentials[index].shared_key) {
228
									usedKey = EncryptService.decryptString(angular.copy(_selected_credentials[index].shared_key), oldVaultPass);
229
								}
230
							}
231
232
							CredentialService.reencryptCredential(_selected_credentials[index].guid, usedKey, newVaultPass).progress(function (data) {
233
								$scope.cur_state = data;
234
							}).then(function () {
235
								var percent = index / _selected_credentials.length * 100;
236
								$scope.change_pw = {
237
									percent: percent,
238
									done: index + 1,
239
									total: _selected_credentials.length
240
								};
241
								if (index < _selected_credentials.length - 1) {
242
									changeCredential(index + 1, oldVaultPass, newVaultPass);
243
								} else {
244
									vault.private_sharing_key = EncryptService.decryptString(angular.copy(vault.private_sharing_key), oldVaultPass);
245
									vault.private_sharing_key = EncryptService.encryptString(vault.private_sharing_key, newVaultPass);
246
									VaultService.updateSharingKeys(vault).then(function () {
247
										$rootScope.$broadcast('logout');
248
										NotificationService.showNotification($translate.instant('login.new.pass'), 5000);
249
									});
250
								}
251
							});
252
						};
253
						changeCredential(0, VaultService.getActiveVault().vaultKey, newVaultPass);
254
255
					});
256
				};
257
258
				$scope.confirm_vault_delete = false;
259
				$scope.delete_vault_password = '';
260
				$scope.delete_vault = function () {
261
					if ($scope.confirm_vault_delete && $scope.delete_vault_password === VaultService.getActiveVault().vaultKey) {
262
						getCurrentVaultCredentials(function (vault) {
263
							var credentials = vault.credentials;
264
							$scope.remove_pw = {
265
								percent: 0,
266
								done: 0,
267
								total: vault.credentials.length
268
							};
269
							var deleteCredential = function(index){
270
								$scope.translationData = {
271
									password:  credentials[index].label
272
								};
273
								CredentialService.destroyCredential(credentials[index].guid).then(function () {
274
									var percent = index / vault.credentials.length * 100;
275
									$scope.remove_pw = {
276
										percent: percent,
277
										done: index,
278
										total: vault.credentials.length
279
									};
280
									if(index === credentials.length-1){
281
										VaultService.deleteVault(vault).then(function () {
282
											SettingsService.setSetting('defaultVaultPass', false);
283
											SettingsService.setSetting('defaultVault', null);
284
											$rootScope.$broadcast('logout');
285
											$location.path('/');
286
										});
287
										return;
288
									}
289
									deleteCredential(index+1);
290
								});
291
							};
292
							deleteCredential(0);
293
						});
294
					}
295
296
				};
297
298
				$rootScope.$on('logout', function () {
299
					$scope.active_vault = null;
300
					VaultService.setActiveVault(null);
301
					$location.path('/');
302
303
				});
304
305
				$scope.cancel = function () {
306
					$location.path('/vault/' + $routeParams.vault_id);
307
				};
308
309
			}]);
310
311
}());